package com.wlyuan.open.remote.facade.translator;

import com.wlyuan.area.dto.AnalyzerAddressDTO;
import com.wlyuan.area.dto.AreaDetailDTO;
import com.wlyuan.area.dto.parse.AreaParseDTO;
import com.wlyuan.open.remote.facade.Address;
import com.wlyuan.open.remote.facade.AddressDTO;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**
 * @author lijing
 */
public class AddressTranslator {
    @Mapper
    public interface Assembler {

        @Mappings({
                @Mapping(source = "locationType", target = "type"),
                @Mapping(source = "longitude", target = "lon"),
                @Mapping(source = "latitude", target = "lat"),
        })
        Address assembler(AreaDetailDTO source);

        AnalyzerAddressDTO assembler(AddressDTO source);

        @Mappings({
                @Mapping(source = "locationType", target = "type"),
                @Mapping(source = "longitude", target = "lon"),
                @Mapping(source = "latitude", target = "lat"),
        })
        Address assemblerParse(com.wlyuan.area.dto.parse.AreaDetailDTO source);

        AreaParseDTO assemblerParse(AddressDTO source);

    }

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    public static Address assembler(AreaDetailDTO source) {
        return ASSEMBLER.assembler(source);
    }

    public static Address assemblerParse(com.wlyuan.area.dto.parse.AreaDetailDTO source) {
        return ASSEMBLER.assemblerParse(source);
    }

    public static AnalyzerAddressDTO assembler(AddressDTO source) {
        var assembler = ASSEMBLER.assembler(source);
        if (StringUtils.isNotBlank(source.getProvince()) && source.getProvince().equals("0")) {
            assembler.setProvinceName("");
        }
        if (StringUtils.isNotBlank(source.getCity()) && source.getCity().equals("0")) {
            assembler.setCityName("");
        }
        if (StringUtils.isNotBlank(source.getDistrict()) && source.getDistrict().equals("0")) {
            assembler.setDistrictName("");
        }
        return assembler;
    }

    public static AreaParseDTO assemblerParse(AddressDTO source) {
        var assembler = ASSEMBLER.assemblerParse(source);
        if (StringUtils.isNotBlank(source.getProvince()) && source.getProvince().equals("0")) {
            assembler.setProvinceName("");
        }
        if (StringUtils.isNotBlank(source.getCity()) && source.getCity().equals("0")) {
            assembler.setCityName("");
        }
        if (StringUtils.isNotBlank(source.getDistrict()) && source.getDistrict().equals("0")) {
            assembler.setDistrictName("");
        }
        return assembler;
    }

}
